package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.mapper.ApUnlikesBehaviorMapper;
import com.heima.behavior.service.IApUnlikesBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 *
 * @author heima
 * @since 2021-03-23
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior> implements IApUnlikesBehaviorService {

    @Override
    public void saveUnLikeBehavior(UnLikesBehaviorDto dto) {
        Boolean isUser=false;
        //1、获取用户id，判断是用户操作还是设备操作
        Integer userId = UserThreadLocalUtils.getUserId();
        if(userId!=null && userId!=0){
            //用户登录，是用户的操作
            isUser=true;
        }
        //2、参数校验
        if(userId==null && dto.getEquipmentId()==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //3、查询当前用户对该文章有过不喜欢行为操作
        QueryWrapper<ApUnlikesBehavior> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUnlikesBehavior::getArticleId,dto.getArticleId());
        if(isUser){
            queryWrapper.lambda().eq(ApUnlikesBehavior::getApUserId,userId);
        }else{
            queryWrapper.lambda().eq(ApUnlikesBehavior::getEquipmentId,dto.getEquipmentId());
        }
        ApUnlikesBehavior unlikesBehavior = this.getOne(queryWrapper);
        if(unlikesBehavior==null){
            //保存不喜欢行为
            unlikesBehavior = new ApUnlikesBehavior();
            unlikesBehavior.setArticleId(dto.getArticleId());
            if(isUser){
                unlikesBehavior.setApUserId(userId);
            }else{
                unlikesBehavior.setEquipmentId(dto.getEquipmentId());
            }
            unlikesBehavior.setType(dto.getType());
            unlikesBehavior.setCreatedTime(new Date());
            this.save(unlikesBehavior);
        }else{
            //更新不喜欢行为操作
            unlikesBehavior.setType(dto.getType());
            unlikesBehavior.setUpdateTime(new Date());
            this.updateById(unlikesBehavior);
        }
    }

    @Override
    public ApUnlikesBehavior queryUnLikeBehavior(Integer userId, String equipmentId, Long articleId) {
        Boolean isUser=false;
        if(userId!=null && userId!=0){
            isUser=true;
        }
        QueryWrapper<ApUnlikesBehavior> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUnlikesBehavior::getArticleId,articleId);
        if(isUser){
            queryWrapper.lambda().eq(ApUnlikesBehavior::getApUserId,userId);
        }else{
            queryWrapper.lambda().eq(ApUnlikesBehavior::getEquipmentId,equipmentId);
        }
        ApUnlikesBehavior unlikesBehavior = this.getOne(queryWrapper);
        return unlikesBehavior;
    }
}
